package com.example.dairenkun.neufoodshop.adapter;

import android.content.Context;
;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.example.dairenkun.neufoodshop.R;
import com.example.dairenkun.neufoodshop.activity.FoodActivity;
import com.example.dairenkun.neufoodshop.bean.Shop;
import com.squareup.picasso.Picasso;

import java.util.List;


public class ShopListAdapter extends RecyclerView.Adapter<ViewHolder> {
    private Context mContext;
    private List mDataList;
    private LayoutInflater mLayoutInflater;
    public ShopListAdapter(Context mContext, List mDataList){
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_shop, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Shop entity = (Shop) mDataList.get(position);
        if (null == entity)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.shop_name.setText(entity.getShopname());
        viewHolder.shop_site.setText(entity.getAddress());
        viewHolder.shop_star.setRating(entity.getLevel());
        if (!"".equals(entity.getPic()))
        Picasso.with(mContext).load(entity.getPic()).into(viewHolder.shop_image);
       viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent=new Intent(mContext,FoodActivity.class);
                intent.putExtra("shop_id",entity.getShop_id()+"");
                intent.putExtra("shopname",entity.getShopname());
                intent.putExtra("shopphone",entity.getPhonenum());
                intent.putExtra("shopintro",entity.getIntro());
                intent.putExtra("shopstar",entity.getLevel()+"");
                mContext.startActivity(intent);

            }
        });
    }


    @Override
    public int getItemCount() {
        return mDataList.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView shop_name;
        ImageView shop_image;
        TextView shop_site;
        RatingBar shop_star;

        public ViewHolder(View itemView) {
            super(itemView);
            shop_name = (TextView) itemView.findViewById(R.id.shop_name);
            shop_image = (ImageView) itemView.findViewById(R.id.shop_image);
            shop_site = (TextView) itemView.findViewById(R.id.shop_site);
            shop_star= (RatingBar) itemView.findViewById(R.id.shop_star);

        }
    }
}
